#!/bin/bash
root_dir="/usr/local/edh"
if [ ! -d "$root_dir" ];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING $root_dir undetected"
  mkdir -p $root_dir
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO creating $root_dir success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR creating $root_dir fail"
    exit 1
  fi
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir detected"
fi

if [ ! -d "$root_dir/jdk" ];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir/jdk undetected"
  tar -zxvf jdk-17.0.12_linux-x64_bin.tar.gz  -C "$root_dir"
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO unzip $script_dir/jdk-17.0.12_linux-x64_bin.tar.gz success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR unzip $script_dir/jdk-17.0.12_linux-x64_bin.tar.gz fail"
    return
  fi
  mv "$root_dir/jdk-17.0.12" "$root_dir/jdk"
  echo "export JAVA_HOME=$root_dir/jdk" >> ~/.bashrc
  echo 'export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH' >> ~/.bashrc
  echo 'export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH' >> ~/.bashrc
  echo "export JAVA_HOME=$root_dir/jdk" >> /etc/profile
  echo 'export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH' >> /etc/profile
  echo 'export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH' >> /etc/profile
  source ~/.bashrc
  source /etc/profile
  java -version
else
  source ~/.bashrc
  source /etc/profile
  java -version
fi




